<?php
namespace app\api\model;
use think\Db;

class AdvertiserModel extends \app\common\model\CommonModel
{
    public function get_access_token($plat)
    {
        switch ($plat) {
            case 'oceanengine':
                $res = Db::table('auto_token')->where(['username' => '搜秀技术部账户'])->limit(1)->find();
                break;
            case 'qianchuan':
                $res = Db::table('auto_token')->where(['username' => '剧星千川账户'])->limit(1)->find();
                break;
            case 'unidesk':
                $res = Db::table('auto_token')->where(['username' => 'Unidesk'])->limit(1)->find();
                break;
            case 'tencent':
                $res = Db::table('auto_token')->where(['username' => '剧星腾讯账户'])->limit(1)->find();
                break;
            case 'kuaishou':
                $res = Db::table('auto_token')->where(['username' => '剧星快手磁力引擎'])->limit(1)->find();
                break;
            case 'jinniu':
                $res = Db::table('auto_token')->where(['username' => '剧星快手磁力金牛'])->limit(1)->find();
                break;
            default:
                throw new \Exception('plat参数错误!');
                break;
        }
        
        if(empty($res['access_token'])){
            throw new \Exception('获取access_token失败!');
        }
        return $res['access_token'];
    }

    /**
     * @param string $advertiser_id
     */
    public function get_advertiser_info($data)
    {
        try {
            $advertiser_id  = $data['advertiser_id'];
            $plat = !empty($data['plat']) ? $data['plat'] : 'oceanengine';
            $token          = $this->get_access_token($plat);
            
            switch ($plat) {
                case 'oceanengine':
                    $oceanengine    = \app\common\tool\Oceanengine::getInstance();
                    $res            = $oceanengine->get_advertiser_info($token,$advertiser_id);
                    if(isset($res['message']) && $res['message'] == 'OK'){
                        return [
                            'advertiser_id' => $advertiser_id,
                            'advertiser_name' => $res['data'][0]['name'], 
                        ];
                    }else{
                        throw new \Exception('获取失败,请检查广告主ID!');
                    }
                    break;
                case 'qianchuan':
                    $oceanengine    = \app\common\tool\Oceanengine::getInstance();
                    $res            = $oceanengine->get_qianchuan_advertiser_info($token,$advertiser_id);
                    if(isset($res['message']) && $res['message'] == 'OK'){
                        return [
                            'advertiser_id' => $advertiser_id,
                            'advertiser_name' => $res['data'][0]['name'], 
                        ];
                    }else{
                        throw new \Exception('获取失败,请检查广告主ID!');
                    }
                    break;
                case 'unidesk':
                    $unidesk    = \app\common\tool\Unidesk::getInstance();
                    $res        = $unidesk->get_unidesk_advertiser($token,1,1,$advertiser_id);
                    if(isset($res['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'][0])){
                        return [
                            'advertiser_id'     => $res['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'][0]['id'],
                            'advertiser_name'   => $res['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'][0]['name'], 
                        ];    
                    }else{
                        throw new \Exception('获取失败,请检查广告主ID!');    
                    }
                    break;
                case 'tencent':
                    $tencent    = \app\common\tool\Tencent::getInstance();
                    $res        = $tencent->get_advertiser_accounts($token,$advertiser_id);
                    if(isset($res[0]['account_id']) && isset($res[0]['account_name'])){
                        return [
                            'advertiser_id'     => $res[0]['account_id'],
                            'advertiser_name'   => $res[0]['account_name'], 
                        ];    
                    }else{
                        throw new \Exception('获取失败,请检查广告主ID!');    
                    }
                    break;
                case 'kuaishou':
                    $kuaishou   = \app\common\tool\Kuaishou::getInstance();
                    $res        = $kuaishou->get_advertiser_info($token,$advertiser_id);
                    if(isset($res['code']) && $res['code'] == 0){
                        return [
                            'advertiser_id'     => $res['data']['user_id'],
                            'advertiser_name'   => $res['data']['user_name'], 
                            'corporation_name'  => $res['data']['corporation_name'], 
                        ];    
                    }elseif (isset($res['code']) && $res['code'] !== 0 && !empty($res['message'])) {
                        throw new \Exception($res['message']);    
                    }else {
                        throw new \Exception('获取失败,请检查广告主ID!');
                    }
                    break;
                case 'jinniu':
                    $jinniu   = \app\common\tool\Jinniu::getInstance();
                    $res      = $jinniu->get_advertiser_info($token,$advertiser_id);
                    if(isset($res['code']) && $res['code'] == 0){
                        return [
                            'advertiser_id'     => $advertiser_id,
                            'ks_id'             => $res['data']['ks_id'],
                            'advertiser_name'   => $res['data']['user_name'], 
                            'company_name'      => $res['data']['company_name'], 
                        ];    
                    }elseif (isset($res['code']) && $res['code'] !== 0 && !empty($res['message'])) {
                        throw new \Exception($res['message']);    
                    }else {
                        throw new \Exception('获取失败,请检查广告主ID!');
                    }
                    break;
                default:
                    throw new \Exception('plat参数错误!');
                    break;
            }
            
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /******************************************** 2024/8/27 改 多授权账户查询广告主存在与否 ********************************************/
    public function get_access_token_bak($plat)
    {
        switch ($plat) {
            case 'oceanengine': // 1=巨量
                $res = Db::table('auto_token')->where(['platform' => 1])->select();
                break;
            case 'qianchuan':   // 2=千川
                $res = Db::table('auto_token')->where(['platform' => 2])->select();
                break;
            case 'unidesk':     // 4=UD
                $res = Db::table('auto_token')->where(['platform' => 4])->select();
                break;
            case 'tencent':     // 3=腾讯
                $res = Db::table('auto_token')->where(['platform' => 3])->select();
                break;
            case 'kuaishou':    // 5=磁力引擎
                $res = Db::table('auto_token')->where(['platform' => 5])->select();
                break;
            case 'jinniu':      // 6=磁力金牛
                $res = Db::table('auto_token')->where(['platform' => 6])->select();
                break;
            default:
                throw new \Exception('plat参数错误!');
                break;
        }
        if(empty($res)){
            throw new \Exception('获取access_token失败!');
        }
        return $res;
    }

    public function get_advertiser_info_bak($data)
    {
        try {
            $advertiser_id  = $data['advertiser_id'];
            $plat           = !empty($data['plat']) ? $data['plat'] : 'oceanengine';
            $token          = $this->get_access_token_bak($plat);
            switch ($plat) {
                case 'oceanengine':
                    $oceanengine    = \app\common\tool\Oceanengine::getInstance();
                    foreach($token as $key => $value){
                        $res    = $oceanengine->get_advertiser_info($value['access_token'],$advertiser_id);
                        if(isset($res['message']) && $res['message'] == 'OK'){
                            return [
                                'advertiser_id' => $advertiser_id,
                                'advertiser_name' => $res['data'][0]['name'], 
                            ];
                        }
                    }
                    throw new \Exception('获取失败,请检查广告主ID!');
                    break;
                case 'qianchuan':
                    $oceanengine    = \app\common\tool\Oceanengine::getInstance();
                    foreach ($token as $key => $value) {
                        $res    = $oceanengine->get_qianchuan_advertiser_info($value['access_token'],$advertiser_id);
                        if(isset($res['message']) && $res['message'] == 'OK'){
                            return [
                                'advertiser_id' => $advertiser_id,
                                'advertiser_name' => $res['data'][0]['name'], 
                            ];
                        }
                    }
                    throw new \Exception('获取失败,请检查广告主ID!');
                    break;
                case 'unidesk':
                    $unidesk    = \app\common\tool\Unidesk::getInstance();
                    foreach ($token as $key => $value) {
                        $res    = $unidesk->get_unidesk_advertiser($value['access_token'],1,1,$advertiser_id);
                        if(isset($res['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'][0])){
                            return [
                                'advertiser_id'     => $res['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'][0]['id'],
                                'advertiser_name'   => $res['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'][0]['name'], 
                            ];    
                        }   
                    }
                    throw new \Exception('获取失败,请检查广告主ID!'); 
                    break;
                case 'tencent':
                    $tencent    = \app\common\tool\Tencent::getInstance();
                    foreach ($token as $key => $value) {
                        $res    = $tencent->get_advertiser_accounts($value['access_token'],$advertiser_id);
                        if(isset($res[0]['account_id']) && isset($res[0]['account_name'])){
                            return [
                                'advertiser_id'     => $res[0]['account_id'],
                                'advertiser_name'   => $res[0]['account_name'], 
                            ];    
                        }        
                    }
                    throw new \Exception('获取失败,请检查广告主ID!');
                    break;
                case 'kuaishou':
                    $kuaishou   = \app\common\tool\Kuaishou::getInstance();
                    foreach ($token as $key => $value) {
                        $res    = $kuaishou->get_advertiser_info($value['access_token'],$advertiser_id);
                        if(isset($res['code']) && $res['code'] == 0 && !empty($res['data']['user_id']) && !empty($res['data']['user_name']) && !empty($res['data']['corporation_name'])){
                            return [
                                'advertiser_id'     => $res['data']['user_id'],
                                'advertiser_name'   => $res['data']['user_name'], 
                                'corporation_name'  => $res['data']['corporation_name'], 
                            ];    
                        }    
                    }
                    throw new \Exception('获取失败,请检查广告主ID!');
                    break;
                case 'jinniu':
                    $jinniu   = \app\common\tool\Jinniu::getInstance();
                    foreach ($token as $key => $value) {
                        $res    = $jinniu->get_advertiser_info($value['access_token'],$advertiser_id);
                        if(isset($res['code']) && $res['code'] == 0 && !empty($res['data']['ks_id']) && !empty($res['data']['user_name']) && !empty($res['data']['company_name'])){
                            return [
                                'advertiser_id'     => $advertiser_id,
                                'ks_id'             => $res['data']['ks_id'],
                                'advertiser_name'   => $res['data']['user_name'], 
                                'company_name'      => $res['data']['company_name'], 
                            ];    
                        }    
                    }
                    throw new \Exception('获取失败,请检查广告主ID!');
                    break;
                default:
                    throw new \Exception('plat参数错误!');
                    break;
            }
            
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
    /******************************************** 2024/8/27 改 多授权账户查询广告主存在与否 ********************************************/
}